package controllers;

import helpers.FileHelper;
import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Domain;

public class StepServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (name == null || name.isEmpty()) {
			return ;
		}
		
		name = UrlHelper.getMainDomain(name);
		
		if (!UrlHelper.checkDomain(name)) {
			return ;
		}
		
		Domain domain = FileHelper.getDomainByFileName(name);
		
		if (domain != null && !domain.isProcessing()) {
			response.sendRedirect("/check/" + name);
			return;
		}
		
		request.setAttribute("name", name);
		request.setAttribute("title", name + " - Process test site");
		request.setAttribute("keywords", "speedtestlab, speed test, content breakdown, wallterfal page, test performance, speed performance site");
		request.setAttribute("description", "speedtestlab.com provice full info about speed performance a website.");
		try {
			request.getRequestDispatcher("/pages/page_step.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
